package com.ve.usb;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.widget.Toast;

import cn.wch.ch34xuartdriver.CH34xUARTDriver;


public class SerialHelper {
    private static final String ACTION_USB_PERMISSION = "com.ve.usb.permission";
    private BroadcastReceiver usbReceiver;
    private Runnable permissionGrandAction;

    public void setPermissionGrandAction(Runnable permissionGrandAction) {
        this.permissionGrandAction = permissionGrandAction;
    }

    public int getBaudRate() {
        return InitCH340.getBaudRate();
    }


    public int read(byte[] buffer, int length) {
        CH34xUARTDriver driver = InitCH340.getDriver();
        return driver == null ? 0 : driver.ReadData(buffer, length);
    }

    public void send(byte[] bytes, int length) {
        CH34xUARTDriver driver = InitCH340.getDriver();
        if (driver != null) {
            driver.WriteData(bytes, length);
        }
    }

    public static boolean isSerialDevice(UsbDevice device) {
        return (device.getProductId() == 29987 && device.getVendorId() == 6790);
    }

    public void open(Context appcontext) {
        InitCH340.initCH340(appcontext);
    }

    public boolean requestPermission(Context context, UsbDevice device) {
        UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
        if (!manager.hasPermission(device)) {
            usbReceiver = new UsbBroadcastReceiver();
            context.registerReceiver(usbReceiver, new IntentFilter(ACTION_USB_PERMISSION));
            PendingIntent mPendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
            manager.requestPermission(device, mPendingIntent);
            return false;
        } else {
            if (permissionGrandAction != null) {
                permissionGrandAction.run();
            }
            return true;
        }
    }

    public void onActivityDestroy(Context context) {
        if (usbReceiver != null) {
            try {
                context.unregisterReceiver(usbReceiver);
            }catch (Exception e){

            }

        }
    }

    public void close() {
        InitCH340.close();
    }


    public void setBaudRate(int baud) {
        InitCH340.setBaudRate(baud);
    }

    class UsbBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(ACTION_USB_PERMISSION)) {
                boolean granted = intent.getExtras().getBoolean(UsbManager.EXTRA_PERMISSION_GRANTED);
                if (granted) {
                    if (permissionGrandAction != null) {
                        permissionGrandAction.run();
                    }
                } else {
                    Toast.makeText(context, "没有usb权限", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}
